package com.baizhi.test;

import com.baizhi.Application;
import com.baizhi.dao.*;
import com.baizhi.entity.*;
import com.baizhi.service.AdminService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class test5 {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private AdminRoleDao adminRoleDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleResourceDao roleResourceDao;
    @Autowired
    private ResourceDao resourceDao;
    @Test
    public void test1(){
        Admin admin = adminDao.selectByName("zhangsan");
        System.out.println(admin);
//        在根据id 查询管理员角色表  得到管理员的所有角色id
        List<AdminRole> adminRoleList = adminRoleDao.selectById(admin.getId());

//        根据角色id查询到所有的角色名直接封装到set中
        Set<String> roles = new HashSet<>();
        for (AdminRole adminRole : adminRoleList) {
            System.out.println(adminRole);
            Role role = roleDao.selectById(adminRole.getRoleId());
            roles.add(role.getRoleName());
        }

        Set<String> permissions = new HashSet<>();
        for (String roleName : roles) {
//            根据角色名 查询 角色权限表 得到权限Id
            List<RoleResource> roleResources = roleResourceDao.selectList(roleName);
            for (RoleResource roleResource : roleResources) {
                System.out.println(roleResource);
//                根据权限Id 查询权限表得到权限字符串  where resource_type=function

                Resource resource = resourceDao.selectById(roleResource.getResourceId());
                System.out.println(resource);
                if (resource != null) {
                    permissions.add(resource.getResourcePermission());
                }
            }

        }
    }
}

